package com.bookkeeping.bookkeepingapi.model;

import com.bookkeeping.bookkeepingapi.web.vm.BookKeepVM;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Entity
@Table(name="book_keep")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BookKeep  implements Serializable {
    @Id
    //主键自增
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;//主键

    @Column(name = "add_time")
    private LocalDateTime addTime=LocalDateTime.now();

    @Column(name = "update_time")
    private LocalDateTime updateTime=LocalDateTime.now();

    @Column(name = "book_keep_date")
    private LocalDate bookKeepDate;

    @Column
    private Integer year;
    @Column
    private Integer month;
    @Column
    private Integer type;//类型：0 支出 1：收入

    @Column
    private String Cagegory;

    @Column
    private BigDecimal price;

    @Column
    private String remark;//备注

    @ManyToOne(fetch = FetchType.LAZY)//懒加载
    @JoinColumn(name ="user_id")
    private User user;

    public BookKeep(BookKeepVM bookKeepVM, User user){
        this.bookKeepDate = bookKeepVM.getBookKeepDate();
        this.year=bookKeepVM.getBookKeepDate().getYear();
        this.month=bookKeepVM.getBookKeepDate().getMonthValue();
        this.type = bookKeepVM.getType();
        this.Cagegory = bookKeepVM.getCagegory();
        this.price = bookKeepVM.getPrice();
        this.remark = bookKeepVM.getRemark();
        this.user = user;
    }
}
